package br.com.jk.email;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author camilo
 */
public class EmailJDialog extends javax.swing.JDialog {

  private static EmailJDialog dialog;
  private int idEntidade;
  private int tipoEntidade;
  EmailTableModel emailTm = new EmailTableModel();
  EmailController emailCont = new EmailController(this);
  private Frame fr;

 //  Singleton
  public static EmailJDialog getInstance(java.awt.Frame parent, boolean modal, int idEntidade, int tipoEntidade){
    if (dialog == null) {
      dialog = new EmailJDialog(parent, modal, idEntidade, tipoEntidade);
    }
    return dialog;
  }
  
  //  Singleton
  public static EmailJDialog getInstance(java.awt.Frame parent, boolean modal){
    if (dialog == null) {
      dialog = new EmailJDialog(parent, modal);
    }
    return dialog;
  }
  
  /**
   * Creates new form EmailJDialog
   */
  private EmailJDialog(java.awt.Frame parent, boolean modal, int idEntidade, int tipoEntidade) {
    super(parent, modal);
    this.idEntidade = idEntidade;
    this.tipoEntidade = tipoEntidade;
    this.fr = parent;

    initComponents();
    this.setLocationRelativeTo(null);
    jtfIdEntidade.setText(String.valueOf(idEntidade));
    jcbPessoal.requestFocus();
  }

  private EmailJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    this.setLocationRelativeTo(null);
    jtfIdEntidade.setText(String.valueOf(idEntidade));
    jcbPessoal.requestFocus();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do
   * NOT modify this code. The content of this method is always regenerated by the Form
   * Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jtfIdEntidade = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    jtfEmail = new javax.swing.JTextField();
    jcbPessoal = new javax.swing.JCheckBox();
    jPanel2 = new javax.swing.JPanel();
    jbSalvar = new javax.swing.JButton();
    jbCancelar = new javax.swing.JButton();
    jtfNome = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Cadastro de e-mail");
    setFont(new java.awt.Font("Liberation Sans", 0, 10)); // NOI18N
    setModal(true);
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosed(java.awt.event.WindowEvent evt) {
        formWindowClosed(evt);
      }
    });

    jLabel1.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jLabel1.setText("Id Entidade");

    jtfIdEntidade.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfIdEntidade.setEnabled(false);

    jLabel2.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jLabel2.setText("E-mail");

    jtfEmail.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfEmail.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfEmailKeyPressed(evt);
      }
    });

    jcbPessoal.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jcbPessoal.setText("Pessoal");
    jcbPessoal.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jcbPessoalKeyPressed(evt);
      }
    });

    jbSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/jk/imagens/dialog-ok.png"))); // NOI18N
    jbSalvar.setToolTipText("Salvar");
    jbSalvar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbSalvarActionPerformed(evt);
      }
    });

    jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/jk/imagens/dialog-cancel.png"))); // NOI18N
    jbCancelar.setToolTipText("Cancelar");
    jbCancelar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbCancelarActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jbSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbCancelar, jbSalvar});

    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jbCancelar)
          .addComponent(jbSalvar))
        .addContainerGap())
    );

    jtfNome.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfNome.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfNomeKeyPressed(evt);
      }
    });

    jLabel3.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jLabel3.setText("Nome");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jtfEmail)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                  .addComponent(jLabel1)
                  .addComponent(jtfIdEntidade, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbPessoal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              .addComponent(jLabel2)
              .addComponent(jLabel3))
            .addGap(0, 0, Short.MAX_VALUE))
          .addComponent(jtfNome))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(jLabel1)
            .addGap(5, 5, 5)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(jtfIdEntidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(jcbPessoal)))
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jtfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel3)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(0, 7, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
    emailCont.salvar();
  }//GEN-LAST:event_jbSalvarActionPerformed

  private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
    emailCont.cancelar();
  }//GEN-LAST:event_jbCancelarActionPerformed

  private void jtfEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfEmailKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jtfNome.requestFocus();
    } else if (evt.getKeyCode() == KeyEvent.VK_UP) {
      jcbPessoal.requestFocus();
    }
  }//GEN-LAST:event_jtfEmailKeyPressed

  private void jtfNomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfNomeKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      emailCont.salvar();
    } else if (evt.getKeyCode() == KeyEvent.VK_UP) {
      jtfEmail.requestFocus();
    } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jbSalvar.requestFocus();
    }
  }//GEN-LAST:event_jtfNomeKeyPressed

  private void jcbPessoalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbPessoalKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jtfEmail.requestFocus();
    }
  }//GEN-LAST:event_jcbPessoalKeyPressed

  private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    dialog = null;
  }//GEN-LAST:event_formWindowClosed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(EmailJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(EmailJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(EmailJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(EmailJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        EmailJDialog dialog = new EmailJDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JButton jbCancelar;
  private javax.swing.JButton jbSalvar;
  private javax.swing.JCheckBox jcbPessoal;
  private javax.swing.JTextField jtfEmail;
  private javax.swing.JTextField jtfIdEntidade;
  private javax.swing.JTextField jtfNome;
  // End of variables declaration//GEN-END:variables

  public EmailTableModel getEmailTm() {
    return emailTm;
  }

  public void setEmailTm(EmailTableModel emailTm) {
    this.emailTm = emailTm;
  }

  public EmailController getEmailCont() {
    return emailCont;
  }

  public void setEmailCont(EmailController emailCont) {
    this.emailCont = emailCont;
  }

  public JLabel getjLabel1() {
    return jLabel1;
  }

  public void setjLabel1(JLabel jLabel1) {
    this.jLabel1 = jLabel1;
  }

  public JLabel getjLabel2() {
    return jLabel2;
  }

  public void setjLabel2(JLabel jLabel2) {
    this.jLabel2 = jLabel2;
  }

  public JPanel getjPanel1() {
    return jPanel1;
  }

  public void setjPanel1(JPanel jPanel1) {
    this.jPanel1 = jPanel1;
  }

  public JPanel getjPanel2() {
    return jPanel2;
  }

  public void setjPanel2(JPanel jPanel2) {
    this.jPanel2 = jPanel2;
  }

  public JButton getJbCancelar() {
    return jbCancelar;
  }

  public void setJbCancelar(JButton jbCancelar) {
    this.jbCancelar = jbCancelar;
  }

  public JButton getJbSalvar() {
    return jbSalvar;
  }

  public void setJbSalvar(JButton jbSalvar) {
    this.jbSalvar = jbSalvar;
  }

  public JCheckBox getJcbPessoal() {
    return jcbPessoal;
  }

  public void setJcbPessoal(JCheckBox jcbPessoal) {
    this.jcbPessoal = jcbPessoal;
  }

  public JTextField getJtfEmail() {
    return jtfEmail;
  }

  public void setJtfEmail(JTextField jtfEmail) {
    this.jtfEmail = jtfEmail;
  }

  public JTextField getJtfIdEntidade() {
    return jtfIdEntidade;
  }

  public void setJtfIdEntidade(JTextField jtfIdEntidade) {
    this.jtfIdEntidade = jtfIdEntidade;
  }

  public static EmailJDialog getDialog() {
    return dialog;
  }

  public static void setDialog(EmailJDialog dialog) {
    EmailJDialog.dialog = dialog;
  }

  public int getIdEntidade() {
    return idEntidade;
  }

  public void setIdEntidade(int idEntidade) {
    this.idEntidade = idEntidade;
  }

  public int getTipoEntidade() {
    return tipoEntidade;
  }

  public void setTipoEntidade(int tipoEntidade) {
    this.tipoEntidade = tipoEntidade;
  }

  public Frame getFr() {
    return fr;
  }

  public void setFr(Frame fr) {
    this.fr = fr;
  }

  public JLabel getjLabel3() {
    return jLabel3;
  }

  public void setjLabel3(JLabel jLabel3) {
    this.jLabel3 = jLabel3;
  }

  public JTextField getJtfNome() {
    return jtfNome;
  }

  public void setJtfNome(JTextField jtfNome) {
    this.jtfNome = jtfNome;
  }
}
